package com.lzy.cloud.goods.service.GoodsServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.GoodsDto;
import com.lzy.cloud.goods.entity.Goods;
import com.lzy.cloud.goods.mapper.GoodsMapperExt;
import com.lzy.cloud.goods.service.GoodsService;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * Created by WIN7 on 2018/6/6.
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsMapperExt goodsMapperExt;

    @Override
    public Long add(GoodsDto dto) {
        Goods goods = DozerUtils.map(dto, Goods.class);
        return goodsMapperExt.insertSelective(goods)>0?goods.getId():null;
    }

    @Override
    public Boolean del(Long id) {
        return goodsMapperExt.deleteByPrimaryKey(id)>0;
    }

    @Override
    public Boolean updateById(GoodsDto dto) {
        return goodsMapperExt.updateByPrimaryKeySelective(DozerUtils.map(dto,Goods.class))>0;
    }

    @Override
    public Boolean updateByCode(GoodsDto dto) {
        return goodsMapperExt.updateByCode(dto)>0;
    }

    @Override
    public GoodsDto selectById(Long id) {
        return DozerUtils.map(goodsMapperExt.selectByPrimaryKey(id),GoodsDto.class);
    }

    @Override
    public PageInfo<GoodsDto> selectList(Pager pager, GoodsDto dto) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        return new PageInfo<>(goodsMapperExt.selectList(dto));
    }

    @Override
    public PageInfo<GoodsDto> selectListByType(Pager pager,Integer type) {
        if (pager != null)
            PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        return new PageInfo<>(goodsMapperExt.selectListByType(type));
    }

    @Override
    public GoodsDto selectByCode(String code) {
        return goodsMapperExt.selectByCode(code);
    }

    @Override
    public Boolean deleteById(String ids) {
        String[] idx = ids.trim().split(",");
        for (String id:idx) {
            goodsMapperExt.deleteById(Long.valueOf(id));
        }
        return true;
    }

    @Override
    public Boolean recoveryById(String ids) {
        String[] idx = ids.trim().split(",");
        for (String id:idx) {
            goodsMapperExt.recoveryById(Long.valueOf(id));
        }
        return true;
    }

    @Override
    public Boolean deleteByKey(Long id) {
        return goodsMapperExt.deleteById(id)>0;
    }

    @Override
    public GoodsDto selectLast(Long shopId) {
        return goodsMapperExt.selectLast(shopId);
    }
}
